package com.prs.crm.action.sys;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sys.PermissionGroup;
import com.prs.crm.service.sys.PermissionGroupService;

public class PermissionGroupAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private PermissionGroup permissionGroup;
	private PermissionGroupService permissionGroupService;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public PermissionGroup getPermissionGroup() {
		return permissionGroup;
	}
	public void setPermissionGroup(PermissionGroup permissionGroup) {
		this.permissionGroup = permissionGroup;
	}
	public PermissionGroupService getPermissionGroupService() {
		return permissionGroupService;
	}
	public void setPermissionGroupService(
			PermissionGroupService permissionGroupService) {
		this.permissionGroupService = permissionGroupService;
	}
	
	@Action("listPermissionGroup")
	public String listPermissionGroup(){
		DetachedCriteria criteira=this.getPaginationHelper().createQuery(PermissionGroup.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPermissionGroupService().listByPage(criteira, pagination));
		return SUCCESS;
	}
	
	@Action("editPermissionGroup")
	public String editPermissionGroup(){
		if (ids!=null) {
			this.permissionGroup=this.getPermissionGroupService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	@Action("editPermissionGroupPerson")
	public String editPermissionGroupPerson(){
		if (ids!=null) {
			this.permissionGroup=this.getPermissionGroupService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action("editPermissionGroupPersons")
	public String editPermissionGroupPersons(){
		if (ids!=null) {
			this.permissionGroup=this.getPermissionGroupService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	@Action(value="savePermissionGroup",results={
			@Result(name="success",type="redirect",location="listPermissionGroup"),
			@Result(name="error",type="chain",location="editPermissionGroup")
	})
	public String savePermissionGroup(){
		this.getPermissionGroupService().save(permissionGroup);
		return SUCCESS;
	}
	
	@Action(value="savePermissionGroups",results={
			@Result(name="success",type="redirect",location="editPermissionGroupPersons?permissionGroup=${permissionGroup.getId()}"),
			@Result(name="error",type="chain",location="editPermissionGroup")
	})
	public String savePermissionGroups(){
		System.out.println(permissionGroup.getName().length());
		if(permissionGroup.getName().length()==0){
			System.out.println(permissionGroup.getName().length());
			this.addActionError("请输入权限组名称");
			return ERROR;
		}else{
			this.getPermissionGroupService().save(permissionGroup);
		}
		return SUCCESS;
	}
	@Action(value="savePermissionGroupPerson",results={
			@Result(name="success",type="redirect",location="listPermissionGroup"),
			@Result(name="error",type="chain",location="editPermissionGroupPerson")
	})
	public String savePermissionGroupPerson(){
		if (ids!=null) {
			this.permissionGroup=this.getPermissionGroupService().get(ids[0]);
		}
		this.getPermissionGroupService().save(permissionGroup);
		return SUCCESS;
	}
	
	@Action(value="savePermissionGroupPersons",results={
			@Result(name="success",type="redirect",location="editPermissionGroup?permissionGroup=${permissionGroup.getId()}"),
			@Result(name="error",type="chain",location="editPermissionGroupPersons?permissionGroup=${permissionGroup.getId()}")
	})
	public String savePermissionGroupPersons(){
		this.getPermissionGroupService().save(permissionGroup);
		return SUCCESS;
	}
	
	@Action(value="removePermissionGroup",results={
			@Result(name="success",type="redirect",location="listPermissionGroup"),
			@Result(name="error",type="chain",location="listPermissionGroup")
	})
	public String removePermissionGroup(){
		if (ids!=null) {
			this.permissionGroup=this.getPermissionGroupService().get(ids[0]);
		}
		this.getPermissionGroupService().batchRemove(ids);
		return SUCCESS;
	}
}
